package com.tanhua.manager.listener;

import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.template.BaiduCensorTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@RocketMQMessageListener(topic = "tanhua-publish",consumerGroup = "tanhua-publish-group")
public class PublishMessageListener implements RocketMQListener<String> {
    @Autowired
    private BaiduCensorTemplate baiduCensorTemplate;

    @Reference
    private PublishApi publishApi;

    @Override
    public void onMessage(String publishId) {
        Publish publish = publishApi.findById(publishId);
        log.info("接收到的消息:   "+publish);

        try {
        if (publish.getState() == 0){
            boolean textCensor = baiduCensorTemplate.textCensor(publish.getTextContent());

            boolean imageCensor = baiduCensorTemplate.imageBatchCensor(publish.getMedias());


            publish.setState(textCensor&&imageCensor?1:2);
            publishApi.update(publish);
        }
        } catch (Exception e) {
            log.error("错误消费消息");
        }

    }
}
